/*
 * 03结构体数组.c
 *
 *  Created on: 2024年3月14日
 *      Author: HP
 */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>

// 重名没事，就近原则
struct student {
	// 结构体成员需要偏移对齐，所以name占了21，那么age就从24开始
	char name[21]; // 字符数组都是地址
	int age;
	char sex;
	int score[3];
	char addr[51];
};

int main0903(){
	struct student stu[3] = {
		{"张三", 22, 'M', 88,79,0, "河北"},
		{"张三2", 22, 'M', 100,99,20, "河北2"},
		{"张三3", 22, 'M', 98,60,10, "河北3"}
	};

	for(int i=0; i<3; i++){
		printf("姓名：%s\n", stu[i].name);
		printf("年龄：%d\n", stu[i].age);
		printf("性别：%s\n", stu[i].sex == 'M'?"男":"女");
		printf("成绩：%d\n", stu[i].score[0]);
		printf("成绩1：%d\n", stu[i].score[1]);
		printf("成绩2：%d\n", stu[i].score[2]);
		printf("地址：%s\n", stu[i].addr);
	}

	// 结构体成员需要偏移对齐
	printf("结构体数组大小：%d\n", sizeof(stu));
	printf("结构体元素大小：%d\n", sizeof(stu[0]));
	printf("结构体数组大小：%d\n", sizeof(stu)/sizeof(stu[0]));

	return 0;
}

int main090301(){
	struct student stu[3] = {
		{"张三", 18, 'M', 88,79,0, "河北"},
		{"张三2", 30, 'M', 100,99,20, "河北2"},
		{"张三3", 22, 'M', 98,60,10, "河北3"}
	};
	for(int i=0; i<3-1; i++){
		for(int j=0; j<3-1-i; j++){
			if(stu[j].age > stu[j+1].age){
				struct student temp = stu[j];
				stu[j] = stu[j+1];
				stu[j+1] = temp;
			}
		}
	}
}
